import { json } from 'body-parser'
import * as compression from 'compression'
import { WinstonModule } from 'nest-winston'
import i18n from '@/i18n'
import { ActionSocketAdapter, AppLogger, SwaggerFactory } from '@libs/lhy-common'
import { CoreConfigService } from '@libs/lhy-core'
import { VersioningType } from '@nestjs/common'
import { NestFactory } from '@nestjs/core'
import { AppModule } from './app.module'
import { LOGS_STORE_ROOT } from './config/app-const'

async function bootstrap() {
  const app = await NestFactory.create(AppModule, {
    logger: WinstonModule.createLogger({ instance: AppLogger.create({ storeRoot: LOGS_STORE_ROOT }) }),
  })

  const configService = app.get(CoreConfigService)
  const port = configService.config.server.port || 3000
  const logLevel = configService.config.log.level || 'error'

  app.use(compression())
  app.use(json({ limit: '20mb' }))
  // app.use(createSession())
  app.useWebSocketAdapter(new ActionSocketAdapter(app))
  app.enableCors()
  app.enableVersioning({
    type: VersioningType.URI,
  })
  SwaggerFactory.setup(app)

  await app.listen(port)
  AppLogger.info('Main', i18n.t('server.src.main.1215072-0', { args: [port, logLevel] }))

  AppLogger.setLevel(logLevel)
}

bootstrap()
